package com.ias.agdis_r.fragment.task;

import android.content.Intent;
import android.os.Bundle;
//import android.support.v4.app.Fragment;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.task.TaskInfoActivity;
import com.ias.agdis_r.activity.task.TaskSendOtherActivity;
import com.ias.agdis_r.adapter.FTaskGridViewAdapter;
import com.ias.agdis_r.adapter.FTaskListViewAdapter;
import com.ias.agdis_r.bean.task.Task;
import com.ias.agdis_r.manager.DataManager;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.util.TaskUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by spoon on 2017/3/20.
 */
public class FTaskListFragment extends Fragment {

    private int sortType = TaskUtil.SORT_TIME_DESC;
    private ListView mListView;// 列表
    private GridView mGridView;
    private FTaskListViewAdapter mAdapter;
    private FTaskGridViewAdapter mGridAdapter;
    private View view;
    private RelativeLayout rl_selectMode;
    private ImageView select_cancel, select_confirm;

    public void updateUI(String tag) {
        // TODO Auto-generated method stub
        // 接收刷新信息，刷新页面
        if (tag.equals(MessageModel.ITSD) || tag.equals(MessageModel.TTSD)
                || tag.equals(MessageModel.REFRESH)||tag.equals(MessageModel.TRPT)) {
            checkInfo();
            sort(this.sortType);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.task_flight_tab_01, container, false);

        rl_selectMode = (RelativeLayout) view.findViewById(R.id.rl_selectMode);
        select_cancel = (ImageView) view.findViewById(R.id.iv_select_cancel);
        select_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setMultiselect(false);
                setSinglchoice(false);
            }
        });
        select_confirm = (ImageView) view.findViewById(R.id.iv_select_confirm);
        select_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (JavaBeanManager.getInstance().isGridTaskMode()) {
                    if (mGridAdapter.isMultiselect()) {
                        gotoTransferTask(mGridAdapter.getMultiselectList());
                    } else if (mGridAdapter.isSinglchoice()) {
                        gotoSendtoTask(mGridAdapter.getSinglchoiceList());
                    }
                } else {
                    if (mAdapter.isMultiselect()) {
                        gotoTransferTask(mAdapter.getMultiselectList());
                    } else if (mAdapter.isSinglchoice()) {
                        gotoSendtoTask(mAdapter.getSinglchoiceList());
                    }
                }
            }
        });
        checkInfo();
        setListAdapter();
        return view;
    }

    /**
     * 跳转获取人员列表界面
     */
    private void gotoTransferTask(List<Task> taskList) {
        if (0 == taskList.size()) {
            EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, getString(R.string.chooseTransferTask), false);
            return;
        }
        Intent intent = new Intent(getActivity().getApplicationContext(), TaskSendOtherActivity.class);
        intent.putExtra("type", TaskSendOtherActivity.TYPE_TRANSFER);
        List<String> tnbs = new ArrayList<String>();
        for (int i = 0; i < taskList.size(); i++) {
            tnbs.add(taskList.get(i).getTNB());
        }
        intent.putExtra("tnb", tnbs.toArray(new String[tnbs.size()]));
        startActivity(intent);
        setMultiselect(false);
    }

    /**
     * 跳转获取人员列表界面
     */
    private void gotoSendtoTask(List<Task> taskList) {
        if (0 == taskList.size()) {
            EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, getString(R.string.chooseSendtoTask), false);
            return;
        }
        Intent intent = new Intent(getActivity().getApplicationContext(), TaskSendOtherActivity.class);
        intent.putExtra("type", TaskSendOtherActivity.TYPE_SENDTO);
        List<String> tnbs = new ArrayList<String>();
        for (int i = 0; i < taskList.size(); i++) {
            tnbs.add(taskList.get(i).getTNB());
        }
        intent.putExtra("tnb", tnbs.toArray(new String[tnbs.size()]));
        startActivity(intent);
        setSinglchoice(false);
    }

    public void checkInfo() {
        try {
            if (null != view) {
                TextView noInfo = (TextView) view.findViewById(R.id.tv_noInfo);
                DataManager dm = DataManager.getInstance();
                if (0 == dm.getWorkTaskList().size() && 0 == dm.getReceiveTaskList().size()) {
                    noInfo.setVisibility(View.VISIBLE);
                } else {
                    noInfo.setVisibility(View.GONE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setListAdapter() {
        if (JavaBeanManager.getInstance().isGridTaskMode()) {
            mGridView = (GridView) view.findViewById(R.id.gridView);
            mGridView.setVisibility(View.VISIBLE);
            mGridAdapter = new FTaskGridViewAdapter(getActivity()
                    .getApplicationContext(), false, false);
            mGridView.setAdapter(mGridAdapter);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                        long arg3) {
                    if(mGridAdapter.isMultiselect()){
                        mGridAdapter.setTransferCheck(arg2, !mGridAdapter.getIsTransferCheck(arg2));
                    }else if (mGridAdapter.isSinglchoice()){
                        mGridAdapter.setSendtoCheck(arg2, !mGridAdapter.getIsSendtoCheck(arg2));
                    }else {
                        Intent intent = new Intent(getActivity()
                                .getApplicationContext(), TaskInfoActivity.class);
                        intent.putExtra(MessageModel.TNB, mGridAdapter.getTNB(arg2));
                        startActivity(intent);
                    }
                }
            });
        } else {
            mListView = (ListView) view.findViewById(R.id.ListView);
            mListView.setVisibility(View.VISIBLE);
            mAdapter = new FTaskListViewAdapter(getActivity()
                    .getApplicationContext(), false, false);
            mListView.setAdapter(mAdapter);
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                        long arg3) {
                    if (mAdapter.isMultiselect()) {
                        mAdapter.setTransferCheck(arg2, !mAdapter.getIsTransferCheck(arg2));
                    } else if (mAdapter.isSinglchoice()) {
                        mAdapter.setSendtoCheck(arg2, !mAdapter.getIsSendtoCheck(arg2));
                    } else {
                        Intent intent = new Intent(getActivity()
                                .getApplicationContext(), TaskInfoActivity.class);
                        intent.putExtra(MessageModel.TNB, mAdapter.getTNB(arg2));
                        startActivity(intent);
                    }
                }
            });
        }
    }

    //设置任务移交多选模式
    public void setMultiselect(boolean flag_multiselect){
        if (JavaBeanManager.getInstance().isGridTaskMode()) {
            mGridAdapter.setMultiselect(flag_multiselect);
        } else {
            mAdapter.setMultiselect(flag_multiselect);
        }
        setSelectView(flag_multiselect);
    }

    //设置任务转发单选模式
    public void setSinglchoice(boolean flag_singlchoice){
        if (JavaBeanManager.getInstance().isGridTaskMode()) {
            mGridAdapter.setSinglchoice(flag_singlchoice);
        } else {
            mAdapter.setSinglchoice(flag_singlchoice);
        }
        setSelectView(flag_singlchoice);
    }

    private void setSelectView(boolean isShow){
        if (isShow){
            rl_selectMode.setVisibility(View.VISIBLE);
        }else {
            rl_selectMode.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 增加排序功能
     *
     * @param sortType
     */
    public void sort(int sortType) {
        this.sortType = sortType;
        if (JavaBeanManager.getInstance().isGridTaskMode()) {
            if (mGridAdapter != null) {
                mGridAdapter.setSortType(sortType);
                //ADAPTER排序
                mGridAdapter.notifyDataSetChanged();
            }
        } else {
            if (mAdapter != null) {
                mAdapter.setSortType(sortType);
                //ADAPTER排序
                mAdapter.notifyDataSetChanged();
            }
        }
    }
}
